package com.ttt.gdfood.controller;

import com.ttt.gdfood.entity.Collection;
import com.ttt.gdfood.entity.Contact;
import com.ttt.gdfood.entity.User;
import com.ttt.gdfood.mapper.UserMapper;
import com.ttt.gdfood.service.CollectionService;
import com.ttt.gdfood.service.ContactService;
import com.ttt.gdfood.service.Imp.UserService;
import com.ttt.gdfood.service.ThumbsUpService;
import com.ttt.gdfood.service.ex.InsertException;
import com.ttt.gdfood.service.ex.UsernameDuplicateException;
import com.ttt.gdfood.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController extends BaseController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private ContactService contactService;

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private ThumbsUpService thumbsUpService;

    @GetMapping("/getAll")
    public List<User> getAll() {
        return userMapper.findAll();
    }

    @GetMapping("/getUserById")
    public JsonResult<User> getUserById(@PathParam("id") int id) {
        User data = userService.getUserById(id);
        return new JsonResult<User>(OK, data, "登录成功");
    }

    @PostMapping("/reg")
    public JsonResult<Void> insertUser(@RequestBody User user) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
// 调用业务对象执行注册
            userService.reg(user);
            // 响应成功
            result.setState(200);
            result.setMessage("注册成功");
        } catch (UsernameDuplicateException e) { // 用户名被占用
            result.setState(400);
            result.setMessage("手机账号已被注册");
        } catch (InsertException e) {
// 插入数据异常
            result.setState(500);
            result.setMessage("注册失败，请联系系统管理员");
        }
        return result;
    }

    @PostMapping("/login")
    public JsonResult<User> login(@RequestParam("phone") String phone, @RequestParam("password") String password) {
        User data = userService.login(phone, password);
        return new JsonResult<User>(OK, data, "登录成功");
    }

    @DeleteMapping("/deleteById")
    public JsonResult<Void> deleteById(@PathParam("id") Integer id) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
// 调用业务对象执行注册
            userService.deleteById(id);
            // 响应成功
            result.setState(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
// 删除异常
            result.setState(500);
            result.setMessage("删除失败");
        }
        return result;
    }

    @PutMapping("/update")
    public JsonResult<Void> updateUser(@RequestBody User user) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
// 调用业务对象执行注册
            userService.updateUser(user);
            // 响应成功
            result.setState(200);
            result.setMessage("修改成功");
        } catch (Exception e) {
// 删除异常
            result.setState(500);
            result.setMessage("修改失败");
        }
        return result;
    }

    @PutMapping("/updateInfo")
    public JsonResult<Void> updateUserInfo(@RequestBody User user) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
// 调用业务对象执行注册
            userService.updateUserInfo(user);
            // 响应成功
            result.setState(200);
            result.setMessage("修改成功");
        } catch (Exception e) {
// 删除异常
            result.setState(500);
            result.setMessage("修改失败");
        }
        return result;
    }

    // 根据用户id获取联系人列表
    @GetMapping("/getContact")
    public List<Contact> getAllContact(@RequestParam("id") int id) {

        return contactService.getContact(id);
    }

    // 根据联系人id删除改联系人
    @DeleteMapping("/delContactById")
    public JsonResult<Void> delContactById(@RequestParam("contact_id") int contact_id) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
// 调用业务对象执行注册
            contactService.delContactById(contact_id);
            // 响应成功
            result.setState(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
// 删除异常
            result.setState(500);
            result.setMessage("删除异常");
        }
        return result;
    }

    // 根据联系人id删除改联系人
    @DeleteMapping("/delContactByCid")
    public JsonResult<Void> delContactByCid(@RequestParam("id") int id) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
// 调用业务对象执行注册
            contactService.delContactByCid(id);
            // 响应成功
            result.setState(200);
            result.setMessage("取关成功");
        } catch (Exception e) {
// 删除异常
            result.setState(500);
            result.setMessage("取关异常");
        }
        return result;
    }

    // 添加联系人insertContact
    @GetMapping("/insertContact")
    public JsonResult<Void> insertContact(@RequestParam("uid") int uid, @RequestParam("cid") int cid) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
// 调用业务对象执行注册
            contactService.insertContact(uid, cid);
            // 响应成功
            result.setState(200);
            result.setMessage("关注成功");
        } catch (Exception e) {
// 删除异常
            result.setState(500);
            result.setMessage("关注失败");
        }
        return result;
    }

    // 根据用户id获取收藏笔记列表
    @GetMapping("/getCollection")
    public List<Collection> getAllCollection(@RequestParam("id") int id) {
        return collectionService.getCollection(id);
    }

    // 取消笔记收藏
    @DeleteMapping("/delCollectionByCnid")
    public JsonResult<Void> delCollectionByCnid(@RequestParam("id") int id) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
            // 对象执行注册
            collectionService.delCollectionByCnid(id);
            // 响应成功
            result.setState(200);
            result.setMessage("取消收藏");
        } catch (Exception e) {
            // 删除异常
            result.setState(500);
            result.setMessage("取消异常");
        }
        return result;
    }

    // 添加笔记收藏
    @GetMapping("/insertCollection")
    public JsonResult<Void> insertCollection(@RequestParam("uid") int uid, @RequestParam("cid") int cid) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
// 调用业务对象执行注册
            collectionService.insertCollection(uid, cid);
            // 响应成功
            result.setState(200);
            result.setMessage("收藏成功");
        } catch (Exception e) {
// 删除异常
            result.setState(500);
            result.setMessage("收藏失败");
        }
        return result;
    }

    // 取消笔记点赞
    @DeleteMapping("/delThumbsupByTnid")
    public JsonResult<Void> delThumbsupByTnid(@RequestParam("id") int id) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
            // 对象执行注册
            thumbsUpService.delThumbsupByTnid(id);
            // 响应成功
            result.setState(200);
            result.setMessage("取消点赞");
        } catch (Exception e) {
            // 删除异常
            result.setState(500);
            result.setMessage("取消异常");
        }
        return result;
    }

    // 添加笔记点赞
    @GetMapping("/insertThumbsup")
    public JsonResult<Void> insertThumbsup(@RequestParam("uid") int uid, @RequestParam("nid") int nid) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
// 调用业务对象执行注册
            thumbsUpService.insertThumbsup(uid, nid);
            // 响应成功
            result.setState(200);
            result.setMessage("点赞成功");
        } catch (Exception e) {
// 删除异常
            result.setState(500);
            result.setMessage("点赞失败");
        }
        return result;
    }
}